package com.ycu.jk16.mall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ycu.jk16.mall.domain.Shipping;
import com.ycu.jk16.mall.domain.User;
import com.ycu.jk16.mall.service.ShippingService;
import com.ycu.jk16.mall.service.UserService;
import com.ycu.jk16.mall.util.common.PageUtil;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

/**
 * 
* @ClassName: ShippingController
* @Description: TODO(ShippingController)
* @author 42
* @date 2018年12月11日 下午6:57:28
 */

@Controller
@RequestMapping(value = "/manage/shipping")
public class ShippingController {
	@Autowired
	@Qualifier("shippingService")
	private ShippingService shippingService;
	@Autowired
	private UserService userService;
	
	/**
	 * 
	 * @Title:             add
	 * @Description:       添加地址
	 * @param:             @param userId
	 * @param:             @param name
	 * @param:             @param province
	 * @param:             @param city
	 * @param:             @param address
	 * @param:             @param zip
	 * @param:             @param phone
	 * @param:             @param mobile
	 * @param:             @param response   
	 * @return:         void   
	 * @throws
	 */
	@RequestMapping(value = "/add")
	public void add(Integer userId, String name, String province, String city, String address, String zip, String phone, String mobile,
			HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> indata = new HashMap<String, Object>();
		Shipping shipping = new Shipping();
		User user;
		try {
			user = userService.findUserById(Integer.valueOf(userId)); //find userId
			user.getId(); //如果没有对应的用户跳到catch
			
			shipping.setUser(user);
			shipping.setName(name);
			shipping.setProvince(province);
			shipping.setCity(city);
			shipping.setAddress(address);
			shipping.setZip(zip);
			shipping.setPhone(phone);
			shipping.setMobile(mobile);
			
			shippingService.insertShipping(shipping);
			
			indata.put("shippingId", shipping.getId());
			data.put("status", 0);
			data.put("msg", "新建地址成功");
			data.put("data", indata); 
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "新建地址失败");
		} finally {
			ResponseJsonUtils.json(response, data);	
		}
	}
	
	/**
	 * 
	 * @Title:             delete
	 * @Description:       删除地址
	 * @param:             @param shippingId
	 * @param:             @param response   
	 * @return:        	   void   
	 * @throws
	 */
	@RequestMapping(value = "/del")
	public void delete(Integer shippingId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Shipping shipping;
		try {
			shipping = shippingService.getShippingById(Integer.valueOf(shippingId));
			shipping.getId(); //如果没有对应的地址跳到catch
			
			shippingService.deleteShiping(Integer.valueOf(shippingId));
			
			data.put("status", 0);
			data.put("msg", "删除地址成功");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "删除地址失败");
		} finally {
			ResponseJsonUtils.json(response, data);	
		}
	}
	
	/**
	 * 
	 * @Title:             update
	 * @Description:       更新
	 * @param:             @param shippingId
	 * @param:             @param name
	 * @param:             @param province
	 * @param:             @param city
	 * @param:             @param address
	 * @param:             @param zip
	 * @param:             @param phone
	 * @param:             @param mobile
	 * @param:             @param response   
	 * @return:         void   
	 * @throws
	 */
	@RequestMapping(value = "/update")
	public void update (Integer shippingId, String name, String province, String city, String address, String zip, String phone, String mobile,
			HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Shipping shipping;
		try {
			shipping = shippingService.getShippingById(Integer.valueOf(shippingId));
			shipping.getId(); //如果没有对应的地址跳到catch
			
			shipping.setName(name);
			shipping.setProvince(province);
			shipping.setCity(city);
			shipping.setAddress(address);
			shipping.setZip(zip);
			shipping.setPhone(phone);
			shipping.setMobile(mobile);
			
			shippingService.updateShipping(shipping);
			
			data.put("status", 0);
			data.put("msg", "更新地址成功");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "更新地址失败");
		} finally {
			ResponseJsonUtils.json(response, data);	
		}
	}
	
	/**
	 * 
	 * @Title:             getShipping
	 * @Description:       选中查看具体的地址
	 * @param:             @param shippingId   
	 * @return:            void   
	 * @throws
	 */
	@RequestMapping(value = "/select")  
	public void getShipping(Integer shippingId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<>();
		Shipping shipping;
		
		try {
			shipping = shippingService.getShippingById(Integer.valueOf(shippingId));
			shipping.getId(); //如果没有对应的地址跳到catch  
			
			data.put("status", 0);
			data.put("data", shipping);  
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "查询地址失败");
		} finally {
			ResponseJsonUtils.json(response, data);	
		}
	}
	
	/**
	 * 
	 * @Title:             getShippingList
	 * @Description:       获得地址列表
	 * @param:             @param userId
	 * @param:             @param pageNum
	 * @param:             @param pageSize
	 * @param:             @param response   
	 * @return:            void   
	 * @throws
	 */
	@RequestMapping(value = "/list")  
	public void getShippingList(Integer userId, @RequestParam(value="pageNum", defaultValue="1")int pageNum,@RequestParam(value="pageSize", defaultValue="10") int pageSize, HttpServletResponse response) {  
		Map<String, Object> data = new HashMap<>();
		Map<String, Object> shippingData = new HashMap<>();
		Map<String, Object> params = new HashMap<>();
		
		List<Shipping> shippingList = new ArrayList<>();
		
		try {
			// catch测试块
			PageUtil page = new PageUtil(pageNum, pageSize, shippingService.countShipping(Integer.valueOf(userId)));
			params.put("page", page);
			params.put("userId", userId);
			
			shippingList = shippingService.getShippingList(params);
			shippingList.get(0);
			
			data.put("status", 0);
			
			shippingData.put("pageNum", pageNum);
			shippingData.put("pageSize", pageSize);
			shippingData.put("size", shippingList.size());
			shippingData.put("pages", page.getTotalPage());
			shippingData.put("list", shippingList);
			shippingData.put("firstPage", 1);
			shippingData.put("prePage", page.hasPreviousPage(pageNum) ? pageNum - 1 : pageNum);
			shippingData.put("nextPage", page.hasNextPage(pageNum) ? pageNum + 1 : pageNum);
			shippingData.put("lastPage", page.getTotalPage());
			shippingData.put("isFisrtPage", page.isFirstPage(pageNum));
			shippingData.put("isLastPage", page.isLastPage(pageNum));
			shippingData.put("hasPreviousPage", page.hasPreviousPage(pageNum));
			shippingData.put("hasNextPage", page.hasNextPage(pageNum));
			
			data.put("data", shippingData);
		} catch (IndexOutOfBoundsException e) {
			data.put("status", 1);
			data.put("msg", "查询失败");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "查询失败");
		} finally {
			ResponseJsonUtils.json(response, data); 
		}
	}
	
	
}
